﻿#include "studentmenu.h"
#include "ui_studentmenu.h"
#include <QStringList>
#include <QDebug>
#include <QMessageBox>
//test git
studentMenu::studentMenu(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::studentMenu)
{
    ui->setupUi(this);
    this->model=new QStandardItemModel;
    this->setM=new modelSet;
    this->ui->tableView->setModel(model);
    this->ui->btn_confirm->hide();
    this->ui->btn_quit->hide();
    this->ui->oldPassword->hide();
    this->ui->newPassword->hide();
    connect(ui->tableView->horizontalHeader(),SIGNAL(sectionDoubleClicked(int)),this, SLOT(sortView(int)));
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

}

studentMenu::~studentMenu()
{
    delete ui;
}
//获取登陆时的Id以检索相关信息
void studentMenu::getLoginId(QString loginId)
{
    this->loginId=loginId;
}
//设置学生对象
void studentMenu::setStuObject()
{
    QStringList studentLine;
    QStringList selectedCourse;//选课信息 可以有多个课程
    //还是一样 把文件里的信息按照一行一行的格式读进studentLine 但这里会乱码
    this->op->readFromInfoFile("stuInfo.csv",studentLine);
    //再写一遍注释吧 studentLine 三行信息 length是3 每行的信息储存起来
    for(int i=0;i<studentLine.length();i++)
    {
        //把当前行的信息储存到line
        QString line=studentLine.at(i);
        //把line去掉前后的空格
        line=line.trimmed();
        //csv文件按照空格的方式分开成 返回列表储存到linesplit
        QStringList linesplit=line.split(",");
        //如果找到了登录id对应的用户
        if(loginId==linesplit.at(1))
        {
            //选定学生所在的那一行是linesplit 长度
            for(int j=7;j<linesplit.length();j++)
            {
                //先判断学生选择的课程 B0337
                selectedCourse.append(linesplit.at(j));
            }

            this->stuInfo=new studentInfo;
            //思路就是 选课有多门 先把所有选课的信息丢到selectedCourse 再一起丢进stuInfo的setInfo函数里
            this->stuInfo->setInfo(linesplit.at(0),linesplit.at(1),linesplit.at(3),linesplit.at(4),linesplit.at(5),linesplit.at(6),linesplit.at(2),selectedCourse);

            this->stuInfo->loadAboutInfo();
        }
    }
    this->ui->stuName->setText("欢迎"+this->stuInfo->getName()+"登陆系统！");
}
/*-------------------------------------以下为按钮代码-------------------------------------*/
// 好奇怪 又好了
void studentMenu::on_btn_exit_clicked()
{
    this->stuInfo->saveAllInfo();
    this->close();
}

void studentMenu::on_btn_check_myinfo_clicked()
{
    this->model->clear();
    this->stuInfo->checkMyInfo(this->model,0);
}

void studentMenu::on_btn_display_all_clicked()
{
    this->stuInfo->checkAllCourse(this->model);
}

void studentMenu::on_btn_giveup_course_clicked()
{
    int row=this->ui->tableView->currentIndex().row();
    if(row!=-1)
    {
        if(model->item(row,1)->text().length()==5)
        {
            QModelIndex index=this->model->index(row,0);
            QString selectId=model->data(index).toString();
            if(this->stuInfo->giveUpCourse(selectId))
            {
                QMessageBox::information(this,"注意","退课成功！","确认");
            }
            else
            {
                QMessageBox::critical(this,"错误","退课失败，请检查！","确认");
            }
        }
        else
        {
            QMessageBox::critical(this,"错误","请先选择相应课程！","确认");
        }
    }
    else
    {
        QMessageBox::critical(this,"错误","请先选择相应课程！","确认");
    }
    this->ui->btn_display_course->click();
}

void studentMenu::on_btn_modify_password_clicked()
{
    this->ui->btn_confirm->show();
    this->ui->btn_quit->show();
    this->ui->oldPassword->show();
    this->ui->newPassword->show();
}

void studentMenu::on_btn_confirm_clicked()
{
    QString oldPassword=this->ui->oldPassword->text();
    QString newPassword=this->ui->newPassword->text();
    if(this->stuInfo->modifyPassword(oldPassword,newPassword))
    {
        if(QMessageBox::information(this,"修改成功！","密码修改成功","确认")==0)
        {
            this->ui->btn_confirm->hide();
            this->ui->btn_quit->hide();
            this->ui->oldPassword->hide();
            this->ui->newPassword->hide();
        }
    }
    else
    {
        QMessageBox::critical(this,"修改失败！","旧密码输入错误或新密码不足六位！","确认");
    }
}

void studentMenu::on_btn_select_course_clicked()
{
    int row=this->ui->tableView->currentIndex().row();
    if(row!=-1)
    {
        if(model->item(row,1)->text().length()==5)
        {
            QString selectId=model->data(this->model->index(row,0)).toString();
            QString name=model->data(this->model->index(row,2)).toString();
            QString time=model->data(this->model->index(row,9)).toString();
            if(this->stuInfo->selectCourse(selectId,name,time))
            {
                QMessageBox::information(this,"注意","选课成功！","确认");
                this->on_btn_display_course_clicked();
            }
            else
            {
                QMessageBox::critical(this,"注意","选课冲突！","确认");
            }
        }
        else
        {
            QMessageBox::critical(this,"错误","请先选择相应课程！","确认");
        }
    }
    else
    {
        QMessageBox::critical(this,"错误","请先选择相应课程！","确认");
    }
}

void studentMenu::on_btn_display_course_clicked()
{
    this->model->clear();
    this->stuInfo->checkMyCourse(model);
}

void studentMenu::on_btn_quit_clicked()
{
    this->ui->btn_confirm->hide();
    this->ui->btn_quit->hide();
    this->ui->oldPassword->hide();
    this->ui->newPassword->hide();
}

void studentMenu::sortView(int a)
{
    if(flag)
        this->setM->quickSort(0, model->rowCount()-1,a,model);
    else
        this->setM->quickSort_1(0, model->rowCount()-1,a,model);
    flag=!flag;
}
